package fun.ipconfig.shopping.entity;

import java.util.Collection;
import java.util.Date;
import java.util.stream.Stream;

import lombok.Getter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import lombok.Setter;

/**
 * @author GZSA
 */
@Setter
@Getter
public class UserEntity implements UserDetails {
    /** 名字*/
    String name;
    /**用户*/
    String user;
    /**密码*/
    String passwd; 
    /**账号到期事件*/
    Date accountExpired;  
    /**认证到期时间*/
    Date credentialsExpired; 
    /**0b1：账号锁定    0b10：账号禁用*/
    Integer status; 
    /**授权*/
    String permission; 
    /**角色*/
    String role; 

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (permission == null) {
            return null;
        }
        return Stream.of(permission.split(",")).map(f->(GrantedAuthority) () -> "ROLE_"+f).toList();
    }

    @Override
    public String getPassword() {
        return passwd;
    }

    @Override
    public String getUsername() {
        return user;
    }

    @Override
    public boolean isAccountNonExpired() {
        return accountExpired.getTime() > System.currentTimeMillis();
    }

    @Override
    public boolean isAccountNonLocked() {
        return (status & 0b1) != 0b1;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return credentialsExpired.getTime() > System.currentTimeMillis();
    }

    @Override
    public boolean isEnabled() {
        return (status & 0b10) != 0b10;
    }
}
